


**This do-file creates Figure A1
**This do-file is necessary also for conducting robustness check shown in Table A1 

/* input 
${output_data}501_551_505_frac_50_63_junedecember_maquila_withoutprice
${output_data}exporter_importer_50_63_productlevel_new_junedecember_maquila
*/

/* output
${output}sample_selection/distribution_kept_maquila.png
${output}sample_selection/distribution_kept_nonmaquila.png
${output_data}sampleweight.dta
*/


use ${output_data}501_551_505_frac_50_63_junedecember_maquila_withoutprice, replace
destring hs2, replace force
merge 1:m rfc hs2 year frac maquila using ${output_data}exporter_importer_50_63_productlevel_new_junedecember_maquila
gen hs6=substr(frac, 1,6)
drop if _merge==2
bys rfc hs2 year frac maquila: gen exporter_maquila_count=(_n==1)
gen value_exporter_maquila_temp=valorendolares if exporter_maquila_count==1 
replace value_exporter_maquila_temp=0 if exporter_maquila_count==0 /*avoid double counting*/
egen value_exporter=total(value_exporter_maquila_temp), by(rfc hs2 frac year) /*summing maquila and non-maquila**/
bys rfc hs2 year frac: gen exporter_count=_n
replace exporter_count=0 if exporter_count!=1 
gen value_exporter_temp=value_exporter
replace value_exporter_temp=0 if exporter_count!=1
gen maquilavalue_temp=maquilavalue
replace maquilavalue_temp=0 if exporter_count!=1
collapse  (sum) value_exporter_temp maquilavalue_temp value_exporter_importer, by(rfc hs6 year newclid2 maquila)
bys rfc hs6 year: egen valorendolares=sum(value_exporter_temp)
bys rfc hs6 year: egen maquilavalue=sum(maquilavalue_temp)
bys rfc hs6 year: egen knownvalue=sum(value_exporter_importer)
gen knownratio=knownvalue/valorendolares
gen known=0
replace known=1 if knownratio>=0.8
collapse known valorendolares maquilavalue knownvalue, by (rfc hs6 year)
gen knownratio=knownvalue/valorendolares
gen known2=0
replace known2=1 if knownratio>=0.8
assert known==known2
gen lvalor=log(valorendolares)
gen lmaquila=log(maquilavalue)
gen lnonmaquila=log(valorendolares-maquilavalue)

**Creating Figure A1

# delimit;
twoway (kdensity lmaquila if known==0, n(50) lcolor(black) lpattern(dash) )
 (kdensity lmaquila if known==1,  n(50) lcolor(black) lpattern(solid) )
 (kdensity lmaquila, n(50) lcolor(black) lpattern(shortdash)), scheme(s1mono) legend(order(1 "Dropped" 2 "Kept" 3 "All")) ytitle(Density) xtitle(Log maquila trade value);
 graph export "${output}sample_selection/distribution_kept_maquila.png", as(png) width(2550) replace;

 # delimit;
twoway (kdensity lnonmaquila if known==0, n(50) lcolor(black) lpattern(dash) )
 (kdensity lnonmaquila if known==1,  n(50) lcolor(black) lpattern(solid) )
 (kdensity lnonmaquila,  n(50) lcolor(black) lpattern(shortdash)), scheme(s1mono) legend(order(1 "Dropped" 2 "Kept" 3 "All")) ytitle(Density) xtitle(Log non-maquila trade value);
 ;
 graph export "${output}sample_selection/distribution_kept_nonmaquila.png", as(png) width(2550) replace;


 #delimit cr
**Constructing Sample Weight used for Figure A1
lowess known lmaquila if year==2004, gen(prob_maquila)
lowess known lnonmaquila if year==2004, gen(prob_nonmaquila2)
replace prob_maquila=0 if prob_maquila==.
replace prob_nonmaquila=0 if prob_nonmaquila==.
replace maquilavalue=0 if maquilavalue==.
gen share_maq=maquilavalue/valorendolares
gen share_nonmaq=(valorendolares-maquilavalue)/valorendolares
gen sampleprob=prob_maquila*share_maq+prob_nonmaquila*share_nonmaq
replace sampleprob=. if year!=2004
save "${output_data}sampleweight", replace




